CREATE trigger RegistrarPecosa
   on [dbo].[DETALLE_PECOSA]
   for Insert  
   as
	   declare
	   @ProductoId int,
	   @Fecha date
	   set @ProductoId = (SELECT IdProducto  FROM INSERTED )
	   set @Fecha = (SELECT CONVERT(date, Fecha) FROM INSERTED DP INNER JOIN PECOSA P ON DP.IdPecosa = P.ID)
	   IF ( @ProductoId  = (SELECT IdProducto  FROM KARDEX where IdProducto  = @ProductoId)and 
			@Fecha = (SELECT CONVERT(date, Fecha) FROM KARDEX where IdProducto = @ProductoId and IdDetPecosa is not null) and 
			(SELECT IdDetPecosa FROM KARDEX where IdProducto = @ProductoId and IdDetPecosa is not null) is not null )
		begin
			UPDATE KARDEX 
			SET 
			CantSalida = CantSalida + (SELECT Cantidad FROM INSERTED), 
			NewCantidad = NewCantidad - (SELECT Cantidad FROM INSERTED)
		end
	   
	   ELSE
	   begin
	   INSERT INTO KARDEX( IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
						Documento, Fecha, Costo, 
						CantEntrada,CantSalida, NewCantidad)
			   SELECT pecosa.IdProducto, NULL, pecosa.ID, 
			   PC.NroDoc, GETDATE(), pecosa.ValorUnitario,
			   0, pecosa.Cantidad, P.Stock - pecosa.Cantidad
			   FROM PRODUCTO P INNER JOIN INSERTED pecosa  
			   ON pecosa.IdProducto = P.ID
			   INNER JOIN PECOSA PC on pecosa.IdPecosa = PC.ID
	   end

	   Update P set P.STOCK = P.STOCK - D.Cantidad
			from PRODUCTO as P inner join
			INSERTED as D on D.IdProducto = P.Id


CREATE trigger [dbo].[ActualizarPecosa]
   on [dbo].[DETALLE_PECOSA]
   for Update
   as
   Update P set P.Stock =P.Stock + D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetPecosa = (SELECT DP.ID FROM DELETED DP)
   and IdProducto = (SELECT DP.IdProducto FROM DELETED DP)
   and CantSalida = (SELECT DP.Cantidad FROM DELETED DP)
   and Fecha = (SELECT P.Fecha FROM DELETED DP
   INNER JOIN PECOSA P ON DP.IdPecosa = P.ID)
     
   INSERT INTO KARDEX( IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
   Fecha, Costo, 
   CantEntrada,CantSalida, NewCantidad)
   
   SELECT  pecosa.IdProducto, NULL, pecosa.ID,
   GETDATE(), pecosa.ValorUnitario,
   0 , pecosa.Cantidad, P.Stock - pecosa.Cantidad
   FROM PRODUCTO P INNER JOIN INSERTED pecosa  
   ON pecosa.IdProducto = P.ID
   
   Update P set P.Stock = P.Stock - I.Cantidad
   from PRODUCTO as P inner join
   INSERTED as I on I.IdProducto = P.Id



CREATE trigger [dbo].[EliminarPecosa]
   on [dbo].[DETALLE_PECOSA]
   for Delete
   as
   Update P set P.Stock = P.Stock + D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetPecosa = (SELECT Dp.ID FROM DELETED Dp)
   and IdProducto = (SELECT Dp.IdProducto FROM DELETED Dp)
   and CantSalida = (SELECT Dp.Cantidad FROM DELETED Dp)
   and Fecha = (SELECT P.FechaSalida FROM DELETED Dp 
   INNER JOIN PECOSA P ON Dp.IdPecosa = P.ID)


CREATE trigger [dbo].[RegistrarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Insert
   as
		declare
		@ProductoId int,
		@Fecha date
		set @ProductoId = (SELECT IdProducto  FROM INSERTED )
	    set @Fecha = (SELECT CONVERT(date, Fecha) FROM INSERTED DG INNER JOIN GUIA_ENTRADA G ON DG.IdGuiaEntrada = G.ID)
	    
	    IF ( @ProductoId  = (SELECT IdProducto  FROM KARDEX where IdProducto  = @ProductoId)and 
			@Fecha = (SELECT CONVERT(date, Fecha) FROM KARDEX where IdProducto = @ProductoId and IdDetGuiaEntrada is not null) and 
			(SELECT IdDetGuiaEntrada FROM KARDEX where IdProducto = @ProductoId and IdDetGuiaEntrada is not null) is not null )
		begin
			UPDATE KARDEX 
			SET 
			CantEntrada = CantEntrada + (SELECT Cantidad FROM INSERTED), 
			NewCantidad = NewCantidad + (SELECT Cantidad FROM INSERTED)
		end
	   
	   ELSE
	   begin
			INSERT INTO KARDEX( 
		    IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
		    Documento, Fecha, Costo, 
		    CantEntrada,CantSalida, NewCantidad)
		   
			SELECT 
			guia.IdProducto, guia.ID, NULL,
			G.NroDoc,  GETDATE(), guia.ValorUnitario,
			guia.Cantidad, 0, P.Stock + guia.Cantidad
			FROM PRODUCTO P INNER JOIN INSERTED guia  ON guia.IdProducto = P.ID
			INNER JOIN GUIA_ENTRADA G on guia.IdGuiaEntrada = G.ID
		   
	   end
	   
	   Update P set P.STOCK = P.STOCK + D.Cantidad
		   from PRODUCTO as P inner join
		   INSERTED as D on D.IdProducto = P.Id


CREATE trigger [dbo].[ActualizarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Update
   as
   Update P set P.Stock = P.Stock - D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
  
   DELETE FROM KARDEX
   where IdDetGuiaEntrada = (SELECT DG.ID FROM DELETED DG)
   and IdProducto = (SELECT DG.IdProducto FROM DELETED DG)
   and CantEntrada = (SELECT DG.Cantidad FROM DELETED DG)
   and Fecha = (SELECT G.Fecha FROM DELETED DG
   INNER JOIN GUIA_ENTRADA G ON DG.IdGuiaEntrada = G.ID)
   
   INSERT INTO KARDEX( IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
   Fecha, Costo, 
   CantEntrada,CantSalida, NewCantidad)
   
   SELECT  guia.IdProducto, guia.ID, NULL, 
   GETDATE(), guia.ValorUnitario,
   guia.Cantidad, 0, P.Stock + guia.Cantidad
   FROM PRODUCTO P INNER JOIN INSERTED guia  
   ON guia.IdProducto = P.ID
   
   Update P set P.Stock = P.Stock + I.Cantidad
   from PRODUCTO as P inner join
   INSERTED as I on I.IdProducto = P.Id


CREATE trigger [dbo].[EliminarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Delete
   as
   Update P set P.Stock = P.Stock - D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetGuiaEntrada = (SELECT DG.ID FROM DELETED DG)
   and IdProducto = (SELECT DG.IdProducto FROM DELETED DG)
   and CantEntrada = (SELECT DG.Cantidad FROM DELETED DG)
   and Fecha = (SELECT G.Fecha FROM DELETED DG
   INNER JOIN GUIA_ENTRADA G ON DG.IdGuiaEntrada = G.ID)






CREATE trigger [dbo].[ActualizarPecosa]
   on [dbo].[DETALLE_PECOSA]
   for Update
   as
   Update P set P.Stock =P.Stock + D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetPecosa = (SELECT DP.ID FROM DELETED DP)
   and IdProducto = (SELECT DP.IdProducto FROM DELETED DP)
   and CantSalida = (SELECT DP.Cantidad FROM DELETED DP)
   and Fecha = (SELECT P.Fecha FROM DELETED DP
   INNER JOIN PECOSA P ON DP.IdPecosa = P.ID)
     
   INSERT INTO KARDEX( IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
   Fecha, Costo, 
   CantEntrada,CantSalida, NewCantidad)
   
   SELECT  pecosa.IdProducto, NULL, pecosa.ID,
   GETDATE(), pecosa.ValorUnitario,
   0 , pecosa.Cantidad, P.Stock - pecosa.Cantidad
   FROM PRODUCTO P INNER JOIN INSERTED pecosa  
   ON pecosa.IdProducto = P.ID
   
   Update P set P.Stock = P.Stock - I.Cantidad
   from PRODUCTO as P inner join
   INSERTED as I on I.IdProducto = P.Id




CREATE trigger [dbo].[EliminarPecosa]
   on [dbo].[DETALLE_PECOSA]
   for Delete
   as
   Update P set P.Stock = P.Stock + D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetPecosa = (SELECT Dp.ID FROM DELETED Dp)
   and IdProducto = (SELECT Dp.IdProducto FROM DELETED Dp)
   and CantSalida = (SELECT Dp.Cantidad FROM DELETED Dp)
   and Fecha = (SELECT P.FechaSalida FROM DELETED Dp 
   INNER JOIN PECOSA P ON Dp.IdPecosa = P.ID)



CREATE trigger [dbo].[RegistrarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Insert
   as
      
   INSERT INTO KARDEX( 
   IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
   Fecha, Costo, 
   CantEntrada,CantSalida, NewCantidad)
   
   SELECT 
   guia.IdProducto, guia.ID, NULL, 
   GETDATE(), guia.ValorUnitario,
   guia.Cantidad, 0, P.Stock + guia.Cantidad
   FROM PRODUCTO P INNER JOIN INSERTED guia  
   ON guia.IdProducto = P.ID
   
   Update P set P.STOCK = P.STOCK + D.Cantidad
   from PRODUCTO as P inner join
   INSERTED as D on D.IdProducto = P.Id





CREATE trigger [dbo].[ActualizarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Update
   as
   Update P set P.Stock = P.Stock - D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
  
   DELETE FROM KARDEX
   where IdDetGuiaEntrada = (SELECT DG.ID FROM DELETED DG)
   and IdProducto = (SELECT DG.IdProducto FROM DELETED DG)
   and CantEntrada = (SELECT DG.Cantidad FROM DELETED DG)
   and Fecha = (SELECT G.Fecha FROM DELETED DG
   INNER JOIN GUIA_ENTRADA G ON DG.IdGuiaEntrada = G.ID)
   
   INSERT INTO KARDEX( IdProducto, IdDetGuiaEntrada,IdDetPecosa, 
   Fecha, Costo, 
   CantEntrada,CantSalida, NewCantidad)
   
   SELECT  guia.IdProducto, guia.ID, NULL, 
   GETDATE(), guia.ValorUnitario,
   guia.Cantidad, 0, P.Stock + guia.Cantidad
   FROM PRODUCTO P INNER JOIN INSERTED guia  
   ON guia.IdProducto = P.ID
   
   Update P set P.Stock = P.Stock + I.Cantidad
   from PRODUCTO as P inner join
   INSERTED as I on I.IdProducto = P.Id
   



CREATE trigger [dbo].[EliminarGuiaEntrada]
   on [dbo].[DETALLE_GUIA_ENTRADA]
   for Delete
   as
   Update P set P.Stock = P.Stock - D.Cantidad
   from PRODUCTO as P inner join
   DELETED as D on D.IdProducto = P.Id
   
   DELETE FROM KARDEX
   where IdDetGuiaEntrada = (SELECT DG.ID FROM DELETED DG)
   and IdProducto = (SELECT DG.IdProducto FROM DELETED DG)
   and CantEntrada = (SELECT DG.Cantidad FROM DELETED DG)
   and Fecha = (SELECT G.Fecha FROM DELETED DG
   INNER JOIN GUIA_ENTRADA G ON DG.IdGuiaEntrada = G.ID)

